package utility;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;

public class SortDate implements Comparator<Object> {
	//constants
	private final String emptyField = "-";
	private final String hasStartDate = "hasSDate";
	private final String hasEndDate = "hasEDate";
	private final String hasNoDate = "hasNoDate";
	private final String hasStartTime = "hasSTime";
	private final String hasEndTime = "hasETime";
	private final String hasNoTime = "hasNoTime";
	
	private final int lessThan = -1;
	private final int moreThan = 1;
	private final int sameValue = 0;
	
	//variables
	private Task task1;
	private Task task2;
	private String task1DateType;
	private String task2DateType;
	private String task1TimeType;
	private String task2TimeType;
	
	public int compare(Object o1, Object o2) {
		task1 = (Task) o1;
		task2 = (Task) o2;
		
		task1DateType = getDateType(task1);
		task2DateType = getDateType(task2);
		task1TimeType = getTimeType(task1);
		task2TimeType = getTimeType(task2);
		
		try{
		switch(task1DateType){
		case hasNoDate :
			return compareWithTask2();
		case hasStartDate:
			return compareStartDate();
		case hasEndDate:
			return compareEndDate();	
		}
		}catch(ParseException e){
		System.out.println(e.getMessage());	
		}	
		
		return 0;	
	}

	
	private int compareWithTask2() {
		if(task2DateType.equals(hasNoDate))
			return checkTime();
		else
			return moreThan;
	}

	private int compareStartDate() throws ParseException {
		int temp = 0;
		switch(task2DateType){
		
		case hasNoDate:
			return lessThan;
		
		case hasStartDate:
			temp = compareDate(task1.startDate,task2.startDate);
		if(temp == 0)
			return checkTime();
		else
			return temp;
		
		case hasEndDate:
			temp = compareDate(task1.startDate,task2.endDate);
			if(temp == 0)
				return checkTime();
			else
				return temp;
		}
		
		assert false;
		return temp;
	}
	
	private int compareEndDate() throws ParseException {
		int temp = 0;
		switch(task2DateType){
		
		case hasNoDate:
			return lessThan;
		
		case hasStartDate:
			temp = compareDate(task1.endDate,task2.startDate);
		if(temp == 0)
			return checkTime();
		else
			return temp;
		
		case hasEndDate:
			temp = compareDate(task1.endDate,task2.endDate);
			if(temp == 0)
				return checkTime();
			else
				return temp;
		}
	
		assert false;
		return temp;
	}

	private int checkTime() {
		switch(task1TimeType){
		case hasNoTime:
				if(task2TimeType.equals(hasNoTime))
					return sameValue;
				else
					return moreThan;
		
		case hasStartTime:		
				return compareStartTime();	
		
		case hasEndTime:
				return compareEndTime();	
		}
		assert false;
		return 0;
	}


	private int compareEndTime() {
		switch(task2TimeType){
		case hasNoTime:
			return lessThan;
		case hasStartTime:
			return task1.endTime.compareTo(task2.startTime);
		case hasEndTime:
			return task1.endTime.compareTo(task2.endTime);
		}
		
		assert false;
		return 0;	
	}

	private int compareStartTime() {
		switch(task2TimeType){
		case hasNoTime:
			return lessThan;
		case hasStartTime:
			return task1.startTime.compareTo(task2.startTime);
		case hasEndTime:
			return task1.startTime.compareTo(task2.endTime);
		}
		
		assert false;
		return 0;	
	}

	private String getDateType(Task temp) {
		if(temp.startDate.equals(emptyField) && temp.endDate.equals(emptyField))
			return hasNoDate;
		else if(!temp.startDate.endsWith(emptyField)){
				return hasStartDate;
			}
		else{
			assert !temp.endDate.equals(emptyField); 		
			return hasEndDate;
		}
	}

	private String getTimeType(Task temp) {
		if(temp.startTime.equals(emptyField) && temp.endTime.equals(emptyField))
			return hasNoTime;
		else if(!temp.startTime.endsWith(emptyField)){
				return hasStartTime;
			}
		else{
			assert !temp.endTime.equals(emptyField); 		
			return hasEndTime;
		}
	}

	
	private int compareDate(String date1, String date2)
			throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
		Date date1New = format.parse(date1);
		Date date2New = format.parse(date2);;
		if(date1New.before(date2New))
			return lessThan;
		else if(date1New.after(date2New))
			return moreThan;
		else
			return sameValue;
	}

	
	
}
